package com.axonactive.yolo.service.internal;

import java.util.concurrent.TimeUnit;

import org.atmosphere.cpr.Broadcaster;
import org.atmosphere.cpr.BroadcasterFactory;

public class NotificationService {

	/**
	 * Notify to user
	 * 
	 * @param userId
	 * @param mess
	 * @return true if success, false if user not online or can't send message
	 */
	public static boolean notifyToUser(int userId, String mess) {
		Broadcaster broadcaster;

		if ((broadcaster = BroadcasterFactory.getDefault().lookup(userId)) != null) {
			return broadcaster.broadcast(mess).isDone();
		}

		return false;
	}

	/**
	 * Notify to user every second value
	 * 
	 * @param userId
	 * @param mess
	 * @param second
	 * @return true if success, false if user not online or can't send message
	 */
	public static boolean notifyScheduleToUser(int userId, String mess, int second) {
		Broadcaster broadcaster;

		if ((broadcaster = BroadcasterFactory.getDefault().lookup(userId)) != null) {
			broadcaster.scheduleFixedBroadcast(mess, second, TimeUnit.SECONDS);
			return true;
		}

		return false;
	}

	/**
	 * Notify to all user
	 * 
	 * @param mess
	 * @return true if success, false if can't send message
	 */
	public static boolean notifyToAllUser(String mess) {
		return BroadcasterFactory.getDefault().get().broadcast(mess).isDone();
	}

	/**
	 * Notify to user every second value
	 * 
	 * @param userId
	 * @param mess
	 * @param second
	 * @return true if success, false if can't send message
	 */
	public static boolean notifyScheduleToAllUser(String mess, int second) {
		return BroadcasterFactory.getDefault().get()
				.scheduleFixedBroadcast(mess, second, TimeUnit.SECONDS)
				.isDone();
	}

}
